package com.source.lightfowl.basedata.controller;

import com.alibaba.excel.EasyExcel;
import com.source.lightfowl.DTO.SlaughterHouseDownloadDTO;
import com.source.lightfowl.DTO.SlaughterHouseUploadDTO;
import com.source.lightfowl.VO.SlaughterHouseVO;
import com.source.lightfowl.basedata.entity.SlaughterHouse;
import com.source.lightfowl.basedata.service.SlaughterHouseService;
import com.source.lightfowl.listener.UploadExcelListener;
import com.source.lightfowl.syslog.annotation.LogAnnotation;
import com.source.lightfowl.utils.MyResult;
import com.source.lightfowl.utils.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: cxy
 * @Description: 屠宰市场前端控制器
 */
@Api(value = "屠宰场管理", tags = "屠宰场信息管理")
@RestController
@CrossOrigin
@RequestMapping
public class SlaughterHouseController {

    @Autowired
    private SlaughterHouseService slaughterHouseService;

    /**
     * 下载Excel
     * @param response
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "下载Excel")
    @GetMapping("/slaughterHouse/downloadExcel")
    public MyResult download(HttpServletResponse response) throws IOException {
        DownloadExcelHandler downloadExcelHandler = new DownloadExcelHandler();
        downloadExcelHandler.download(response, slaughterHouseService, SlaughterHouseDownloadDTO.class);
        return new MyResult(0, "success");
    }

    /**
     * 文件下载并且失败的时候返回json（默认失败了会返回一个有部分数据的Excel）
     * @param response
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "文件下载并且失败的时候返回json（默认失败了会返回一个有部分数据的Excel）")
    @GetMapping("/slaughterHouse/downloadFailedUsingJson")
    public MyResult downloadFailedUsingJson(HttpServletResponse response) throws IOException {
        DownloadExcelHandler downloadExcelHandler = new DownloadExcelHandler();
        downloadExcelHandler.downloadFailedUsingJson(response, slaughterHouseService, SlaughterHouseDownloadDTO.class);
        return new MyResult(0, "success");
    }

    /**
     * 上传Excel
     * @param file
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "上传Excel")
    @PostMapping("/slaughterHouse/uploadExcel")
    @ResponseBody
    public MyResult upload(MultipartFile file, HttpServletRequest request) throws IOException {
        EasyExcel.read(file.getInputStream(), SlaughterHouseUploadDTO.class, new UploadExcelListener(slaughterHouseService, request)).sheet().doRead();
        return new MyResult(0, "success");
    }

    //查找全部
    @ApiOperation(value = "查询全部数据" , notes = "- 只查询审核通过的数据")
    @GetMapping("/slaughterHouse")
    public MyResult findAll() {
        List<SlaughterHouseVO> all = slaughterHouseService.findAll(1);
        return MyResult.build(0, "成功", all);
    }

    //查找 分页
    @ApiOperation(value = "分页查询" , notes = "- 只查询审核通过的数据")
    @GetMapping("/slaughterHouse/FindAllPage")
    public MyResult findAllPage(int page, int pageNum) {
        Page<SlaughterHouseVO> pageList = slaughterHouseService.findAllPageVO(1, page, pageNum);
        return MyResult.build(0, "成功", new PageResult<SlaughterHouseVO>(pageList.getTotalElements(), pageList.getContent()));
    }

    //根据id查
    @ApiOperation(value = "根据id查询" , notes = "- 查询所有状态的数据")
    @GetMapping("/slaughterHouse/id")
    public MyResult findById(Integer id) {
        SlaughterHouseVO house = slaughterHouseService.findByIdVO(id);
        return MyResult.build(0, "成功", house);
    }

    //增加
    @ApiOperation(value = "增加 数据 ", notes = "- 等待管理员审核")
    @RequestMapping(value = "/slaughterHouse", method = RequestMethod.POST)
    @LogAnnotation(operEvent = "操作类型：添加", operType = 1)
    public MyResult create(SlaughterHouse house, MultipartFile file, HttpServletRequest request) {
        house.setState(2);
        slaughterHouseService.save(house, file, request);
        return MyResult.build(0, "成功");
    }

    // 修改
    @ApiOperation(value = "修改 数据 ")
    @LogAnnotation(operEvent = "操作类型：修改", operType = 2)
    @RequestMapping(value = "/slaughterHouse/update", method = RequestMethod.POST)
    public MyResult update(SlaughterHouse house, MultipartFile file, HttpServletRequest request) {
        SlaughterHouse oldSlaughterHouse = slaughterHouseService.findById(house.getId()).get();
        house.setState(oldSlaughterHouse.getState());
        slaughterHouseService.save(house, file, request);
        return MyResult.build(0, "成功");
    }

    //删除
    @ApiOperation(value = "根据id删除数据" ,notes = "- 等待管理员审核")
//    @DeleteMapping("/slaughterHouse/id")
    @RequestMapping(value = "/slaughterHouse/id", method = RequestMethod.POST)
    @LogAnnotation(operEvent = "操作类型：删除记录", operType = 3)
    public MyResult deleteById(Integer id) {
        SlaughterHouse slaughterHouse = slaughterHouseService.findById(id).get();
        slaughterHouse.setState(3);
        slaughterHouseService.save(slaughterHouse);
        return MyResult.build(0, "成功");
    }

    //删除 批量
    @ApiOperation(value = "批量删除数据" , notes = "- 参数传递id的字符串 用英文的逗号分开 \n - 等待管理员审核")
    @DeleteMapping("/slaughterHouseDeleteBatch")
    @LogAnnotation(operEvent = "操作类型：批量删除记录", operType = 4)
    public MyResult deleteBatch(String ids) {    //注意：参数的分隔符逗号是英文的
        slaughterHouseService.changeBatch(3, ids);
        return MyResult.build(0, "成功");

    }

    //模糊查询
    @ApiOperation(value = "按名称模糊查询")
    @GetMapping(value = "/slaughterHouse/likeName")
    public MyResult getSlaughterHouseByLikeName(String name) {
        List<SlaughterHouseVO> result = slaughterHouseService.getByLikeName(name);
        return MyResult.build(0, "成功", result);
    }

    /**
     * 按省返回所有市
     *
     * @param province
     * @return
     */
    @ApiOperation(value = "按省返回所有市")
    @GetMapping(value = "/slaughterHouse/findCity")
    public MyResult findCity(String province) {
        List<String> list = slaughterHouseService.findByProvince(province);
        return new MyResult(0, "成功", list);
    }

    /**
     * 按省市返回所有区
     *
     * @param province
     * @param city
     * @return
     */
    @ApiOperation(value = "按省市返回所有区")
    @GetMapping(value = "/slaughterHouse/findDistrict")
    public MyResult findDistrict(String province, String city) {
        List<String> list = slaughterHouseService.findByCity(province, city);
        return new MyResult(0, "成功", list);
    }

    /**
     * 按省市区返回市场
     *
     * @param province
     * @param city
     * @param district
     * @return
     */
    @ApiOperation(value = "按省市区返回市场")
    @GetMapping(value = "/slaughterHouse/findByDistrict")
    public MyResult findByDistrict(String province, String city, String district) {
        List<SlaughterHouse> list = slaughterHouseService.findByDistrict(province, city, district);
        return new MyResult(0, "成功", list);
    }

    /**
     * 管理员审核 - 发布通过
     * @param ids
     * @return
     */
    @ApiOperation(value = "管理员审核 - 发布通过 " , notes = "- 批量审核：参数传递id的字符串 用英文的逗号分开")
    @RequestMapping(value = "/slaughterHouse/agreePublishs", method = RequestMethod.POST)
    public MyResult agreePublishs(String ids) {
        slaughterHouseService.changeBatch(1, ids);
        return MyResult.build(0, "成功");
    }

    /**
     * 管理员审核 - 删除通过
     * @param ids
     * @return
     */
    @ApiOperation(value = "管理员审核 - 删除通过 " , notes = "- 批量审核：参数传递id的字符串 用英文的逗号分开")
    @RequestMapping(value = "/slaughterHouse/agreeDeleteS", method = RequestMethod.POST)
    public MyResult agreeDeleteS(String ids) {
        slaughterHouseService.deleteBatch(0, ids);
        return MyResult.build(0, "成功");
    }

    /**
     * 发布待审核列表
     * @return
     */
    @ApiOperation(value = "发布待审核列表")
    @GetMapping("/slaughterHouse/findAgreeing")
    public MyResult findAgreeing() {
        List<SlaughterHouseVO> list = slaughterHouseService.findAll(2);
        return MyResult.build(0, "成功", list);
    }

    /**
     * 删除待审核列表
     * @return
     */
    @ApiOperation(value = "删除待审核列表")
    @GetMapping("/slaughterHouse/findDeleting")
    public MyResult findDeleting() {
        List<SlaughterHouseVO> list = slaughterHouseService.findAll(3);
        return MyResult.build(0, "成功", list);
    }

    /*@InitBinder//处理前端字符串时间请求与Date不匹配导致的404
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }*/
}
